clc; close all; clear;
addpath('../02_Functions');

%------------------------------------------------------------------------%
% This file creates figure 7 in the paper 
%------------------------------------------------------------------------%

% QUOTES
file_name           = '../01_data/es_quotes_5minute_EST';

% choose sample period
start_date          = datenum(1998,1,5);
finish_date         = datenum(2020,12,29);

% Compute returns
winsorizing         = 0;   
alpha               = 0.99;       
replace_option      = 1; 

results             = make_returns_288(file_name,start_date,finish_date,winsorizing,alpha,replace_option);

% ---------------------------------------------------------------------------------- %

% mean log returns year by year
years = unique(year(results.caldt));

for t = 1 : length(years)
    
    indx = find(year(results.caldt) == years(t));
    
    log_YbY_LO1(t,1)        = nanmean(results.log_return_DbD_LO1(indx));
    [h,p,ci,stats]          = ttest(results.log_return_DbD_LO1(indx));
    p_log_YbY_LO1(t,1)      = p;
    
    log_YbY_LO2(t,1)        = nanmean(results.log_return_DbD_LO2(indx));
    [h,p,ci,stats]          = ttest(results.log_return_DbD_LO2(indx));
    p_log_YbY_LO2(t,1)      = p;
    
    log_YbY_LO(t,1)         = nanmean(results.log_return_DbD_LO(indx));
    [h,p,ci,stats]          = ttest(results.log_return_DbD_LO(indx));
    p_log_YbY_LO(t,1)       = p;
    
    
end

% --------------------------------------------------------------------------------- %
% BAR PLOT YEAR BY YEAR


log_YbY_LO1_an       = 252*100*log_YbY_LO1;
log_YbY_LO2_an       = 252*100*log_YbY_LO2;
log_YbY_LO_an        = 252*100*log_YbY_LO;

p_log_YbY_LO1        = p_log_YbY_LO1;
p_log_YbY_LO2        = p_log_YbY_LO2;
p_log_YbY_LO         = p_log_YbY_LO;


% --------------------------------------------------------------------------------- %

time_span = 1998 : 2 : 2020;
x_ticks = time_span;
x_labels = num2str(time_span');

figure;
p1 = bar(years,log_YbY_LO1_an);
set(p1(1),'FaceColor',rgb('blue'));
ylabel('Return (% p.a)' )
title(' ');
hold on
set(gca,'XTick' , x_ticks)
xticklabels(x_labels)
xtickangle(90)
xlim([(years(1))-0.5 , (years(end))+0.5])
legend off


figure;
p2 = bar(years,1-p_log_YbY_LO1);
set(p2(1),'FaceColor',rgb('blue'));
ylabel('1 - p' )
hold on
plot((years(1)-1):(years(end)+1) , repmat(0.90,1,length(years)+2),'k--','Linewidth',3);
hold on
set(gca,'XTick' , x_ticks)
xticklabels(x_labels)
xtickangle(90)
xlim([(years(1))-0.5 , (years(end))+0.5])
legend off

% --------------------------------------------------------------------------------- %

figure;
p1 = bar(years,log_YbY_LO2_an);
set(p1(1),'FaceColor',rgb('green'));
ylabel('Return (% p.a)' )
title(' ');
hold on
set(gca,'XTick' , x_ticks)
xticklabels(x_labels)
xtickangle(90)
xlim([(years(1))-0.5 , (years(end))+0.5])
legend off

figure;
p2 = bar(years,1-p_log_YbY_LO2);
set(p2(1),'FaceColor',rgb('green'));
ylabel('1 - p' )
hold on
plot((years(1)-1):(years(end)+1) , repmat(0.90,1,length(years)+2),'k--','Linewidth',3);
hold on
set(gca,'XTick' , x_ticks)
xticklabels(x_labels)
xtickangle(90)
xlim([(years(1))-0.5 , (years(end))+0.5])
legend off



% --------------------------------------------------------------------------------- %


figure;
p1 = bar(years,log_YbY_LO_an);
set(p1(1),'FaceColor',rgb('red'));
ylabel('Return (% p.a)' )
title(' ');
hold on
set(gca,'XTick' , x_ticks)
xticklabels(x_labels)
xtickangle(90)
xlim([(years(1))-0.5 , (years(end))+0.5])
legend off


figure;
p2 = bar(years,1-p_log_YbY_LO);
set(p2(1),'FaceColor',rgb('red'));
ylabel('1 - p' )
hold on
plot((years(1)-1):(years(end)+1) , repmat(0.90,1,length(years)+2),'k--','Linewidth',3);
hold on
set(gca,'XTick' , x_ticks)
xticklabels(x_labels)
xtickangle(90)
xlim([(years(1))-0.5 , (years(end))+0.5])
legend off



